package com.ttdevs.tvolley;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.ttdevs.tvolley.adapter.LeftListAdapter;
import com.ttdevs.tvolley.app.VolleyInit;
import com.ttdevs.tvolley.bean.ImageInfo;

/**
 * https://developers.google.com/image-search/v1/jsondevguide?hl=zh-CN&csw=1#
 * using_json
 * https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=fuzzy
 * &rsz=8&start=0
 * 
 * http://image.baidu.com/i?tn=baiduimagejson&width=&height=&word=girl&rn=10&pn=2
 * 
 * @author ttworking
 * 
 */
public class ImageListActivity extends Activity implements OnClickListener {
	private final static String reqUrl = "http://image.baidu.com/i?tn=baiduimagejson&width=&height=&word=girl&rn=10&pn=2";
	private ListView lvLeft, lvRight;
	private LeftListAdapter leftAdapter;

	private ArrayList<ImageInfo> infos;

	private RequestQueue queue;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_image_list);

		queue = VolleyInit.getRequestQueue();

		// lvLeft = (ListView) findViewById(R.id.lvLeft);
		
		infos = new ArrayList<ImageInfo>();

		leftAdapter = new LeftListAdapter(this, infos);
		lvLeft.setAdapter(leftAdapter);
	}

	@Override
	protected void onResume() {
		queue.cancelAll("myTag");
		super.onResume();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btRequest:
			initData();
			break;

		default:
			break;
		}
	}

	private void initData() {
		JsonObjectRequest req = new JsonObjectRequest(reqUrl, null, createMyReqSuccessListener(), createMyReqErrorListener());
		req.setTag("myTag");
		queue.add(req);
	}

	private Response.Listener<JSONObject> createMyReqSuccessListener() {
		return new Response.Listener<JSONObject>() {
			@Override
			public void onResponse(JSONObject response) {
				System.out.println(response.toString());
				try {
					JSONArray data = response.getJSONArray("data");
					for (int i = 0; i < data.length(); i++) {
						JSONObject result = data.getJSONObject(i);
						String objURL = result.getString("objURL");
						System.out.println(">>>>>>objURL:" + objURL);
						if(objURL!=null){
							infos.add(new ImageInfo(objURL));
						}
					}
					
				} catch (JSONException e) {
					e.printStackTrace();
					return;
				}
				leftAdapter.notifyDataSetChanged();
			}
		};
	}

	private Response.ErrorListener createMyReqErrorListener() {
		return new Response.ErrorListener() {
			@Override
			public void onErrorResponse(VolleyError error) {
				System.out.println(error.getMessage());
			}
		};
	}

}
